<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="登录帐号"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.password" type="password" placeholder="新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="comfirmPassword" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <!--      <el-form-item label="租户列表" prop="tenantId">-->
      <!--        <el-select v-model="dataForm.tenantId" placeholder="请选择租户" @change="handlerTenantChange">-->
      <!--          <el-option-->
      <!--            v-for="item in tenantList"-->
      <!--            :key="item.tenantNum"-->
      <!--            :label="item.name"-->
      <!--            :value="item.tenantNum">-->
      <!--          </el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="角色" prop="roleIdList">
        <el-checkbox-group v-model="dataForm.roleIdList">
          <el-checkbox
            v-for="role in roleList"
            :key="role.roleId"
            :label="role.roleId"
          >{{ role.roleName }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      var validatePassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('请输入新密码'))
        } else {
          callback()
        }
      }
      var validateComfirmPassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('确认密码不能为空'))
        } else if (this.dataForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      // var validateEmail = (rule, value, callback) => {
      //   if (!isEmail(value)) {
      //     callback(new Error('邮箱格式错误'))
      //   } else {
      //     callback()
      //   }
      // }
      // var validateMobile = (rule, value, callback) => {
      //   if (!isMobile(value)) {
      //     callback(new Error('手机号格式错误'))
      //   } else {
      //     callback()
      //   }
      // }
      return {
        visible: false,
        roleList: [],
        tenantList: [],
        dataForm: {
          id: 0,
          tenantId: '',
          userName: '',
          password: '',
          comfirmPassword: '',
          salt: '',
          email: '',
          mobile: '',
          roleIdList: [],
          status: 1
        },
        dataRule: {
          userName: [
            {required: true, message: '用户名不能为空', trigger: 'blur'}
          ],
          password: [{validator: validatePassword, trigger: 'blur'}],
          comfirmPassword: [
            {validator: validateComfirmPassword, trigger: 'blur'}
          ]
          // email: [
          //   { required: true, message: '邮箱不能为空', trigger: 'blur' },
          //   { validator: validateEmail, trigger: 'blur' }
          // ],
          // mobile: [
          //   { required: true, message: '手机号不能为空', trigger: 'blur' },
          //   { validator: validateMobile, trigger: 'blur' }
          // ]
        }
      }
    },
    mounted () {
      this.dataForm.tenantId = this.$cookie.get('tenantId')
    },
    methods: {
      getRoleList (tenantId) {
        this.$http({
          url: this.$http.adornUrl('/sys/role/select'),
          method: 'post',
          data: {
            tenantId: tenantId
          }
        }).then(data => {
          this.roleList = data && data.code === 200 ? data.data : []
        })
      },
      init (id) {
        if (this.dataForm.tenantId) {
          this.getRoleList(this.dataForm.tenantId)
        }
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/sys/user/getuserinfo'),
            method: 'post',
            data: {userId: this.dataForm.id}
          }).then(data => {
            if (data && data.code === 200) {
              this.dataForm.userName = data.data.username
              this.dataForm.salt = data.data.salt
              this.dataForm.email = data.data.email
              this.dataForm.mobile = data.data.mobile
              this.dataForm.roleIdList = data.data.roleIdList
              this.dataForm.status = data.data.status
              this.dataForm.tenantId = Number(data.data.tenantId)
              // this.getRoleList(this.dataForm.tenantId)
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(
                `/sys/user/${!this.dataForm.id ? 'save' : 'update'}`
              ),
              method: 'post',
              data: {
                userId: this.dataForm.id || undefined,
                username: this.dataForm.userName,
                password: this.dataForm.password,
                salt: this.dataForm.salt,
                email: this.dataForm.email,
                mobile: this.dataForm.mobile,
                status: this.dataForm.status,
                roleIdList: this.dataForm.roleIdList.join(','),
                tenantId: this.dataForm.tenantId
              }
            }).then(data => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.$message.warning(data.msg)
              }
            })
          }
        })
      },
      handlerTenantChange (tenantId) {
        this.dataForm.tenantId = tenantId
        this.getRoleList(tenantId)
      }
    }
  }
</script>
